//
//  AbstractUI.m
//  企信平台
//
//  Created by Benson Zhang on 13-4-1.
//
//

#import "AbstractUI.h"


@interface AbstractUI ()

@end

@implementation AbstractUI




- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        
//        self.tabBarItem.image = [UIImage imageNamed:@"1.png"];
//        self.tabBarItem.title =@"菜单";
//        self.navigationItem.title = @"菜单";
        [self initNameAndTile];
    }
    return self;
}

-(void) initNameAndTile{
    self.tabBarItem.image = [UIImage imageNamed:@"1.png"];
    self.tabBarItem.title =@"菜单";
    self.navigationItem.title = @"菜单";

}


- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
     [activityIndicatorView stopAnimating] ;
    if (error.code != NSURLErrorCancelled)
    {
        UIAlertView *alterview = [[UIAlertView alloc] initWithTitle:@"" message:@"网络无法连接"  delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
        [alterview show];
    } else if (error.code == NSURLErrorCancelled) {
        if(fabs(errorTime.timeIntervalSinceNow) >1) {
            errorTime = [[NSDate alloc]init];
            [self refreshThisPage];
        }
    }
}

- (void)webViewDidStartLoad:(UIWebView *)webView
{
    [activityIndicatorView startAnimating] ;
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [activityIndicatorView stopAnimating] ;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UIView* parentView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    parentView.autoresizesSubviews = YES;
    
//    CGRect bounds = [[UIScreen mainScreen] applicationFrame];
//        myWebView = [[UIWebView alloc] initWithFrame:bounds];
    myWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    myWebView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
//    [myWebView setAutoresizesSubviews:YES];
//    [myWebView setDataDetectorTypes:UIDataDetectorTypePhoneNumber];
    
    
    activityIndicatorView = [[UIActivityIndicatorView alloc]
                             initWithFrame : CGRectMake(0.0f, 0.0f, 50.0f, 50.0f)] ;
    [activityIndicatorView setCenter: self.view.center] ;
    [activityIndicatorView setActivityIndicatorViewStyle: UIActivityIndicatorViewStyleGray] ;
    //    [activityIndicatorView setHidesWhenStopped:NO];
//    [self.view addSubview : activityIndicatorView] ;
    
    
    [parentView addSubview:myWebView];
    [parentView addSubview:activityIndicatorView];
    self.view =parentView;
//    [self.view addSubview : myWebView] ;
    
    
    myWebView.scalesPageToFit =YES;
    myWebView.delegate =self;
    

    
    
    

    
    
    UIBarButtonItem *rightButton =[self createBtnWithImg :@"refresh.png" action:@selector(refreshThisPage)];
    self.navigationItem.rightBarButtonItem = rightButton;
    
    self.navigationController.navigationBar.tintColor = [UIColor blackColor];
    
    //    NSString *url1 = [URL_PREFIX stringByAppendingFormat:@"%@%@%@", URL1_ACTION, QIYE_ARG, QIYE_ID];
    //    qxUrl = @"http://113.240.221.145/CustomerAppMenu!menu.action?perID=ZLZK0001";
    //     qxUrl = @"http://192.168.1.133/123.htm";
    //qxUrl = @"http://tuping-1982.xicp.net:8080/CustomerAppMenu!menu.action?perID=ZLZK0001";
    qxUrl= [self getCurrentUrlForLoad];
    [self loadWebPageWithString:qxUrl];
    //    [CheckNetwork isExistenceNetwork: qxUrl];
    errorTime = [[NSDate alloc] init];
}

- (NSString *)getCurrentUrlForLoad
{
    return  [self getWebUrl:URL1_ACTION];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)loadWebPageWithString:(NSString*)urlString
{
    NSURL *url =[NSURL URLWithString:urlString];
    NSURLRequest *request =[NSURLRequest requestWithURL:url];
    [myWebView loadRequest:request];
}




- (void)sendSMS :(NSString *)msgBody {
	
	BOOL canSendSMS = [MFMessageComposeViewController canSendText];
	if (canSendSMS) {
        
		MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
		picker.messageComposeDelegate = self;
		picker.navigationBar.tintColor = [UIColor blackColor];
		picker.body = msgBody;
        //		picker.recipients = [NSArray arrayWithObject:@"186-0123-0123"];
		[self presentModalViewController:picker animated:YES];
        
	}
    
}



-(void)refreshThisPage
{
    [myWebView stopLoading];
    [self loadWebPageWithString:qxUrl];
    [super refreshThisPage: myWebView urlFre: qxUrl];
}




-(void)backPage
{
    //    if (![myWebView canGoBack]){
    //        self.navigationItem.leftBarButtonItem = nil;
    //        [self refreshThisPage];
    //    }
    [myWebView stopLoading];
    [myWebView goBack];
    if (![myWebView canGoBack] ||  [webCurrentUrl isEqualToString:qxUrl]){
        self.navigationItem.leftBarButtonItem = nil;
        [self refreshThisPage];
    }
    [self backPage:myWebView curUrl:qxUrl];
    //    [self performSelector: @selector(disableBtn:) withObject:myWebView withObject:webUrl afterDelay:1 ];
    
    //    NSLog(@"------back---%@",myWebView.request.URL.absoluteString);
    
}




- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:
(NSURLRequest*)request navigationType:
(UIWebViewNavigationType)navigationType
{
    
    NSString *urlString = [[request URL] absoluteString];
    webCurrentUrl = urlString;
    if([urlString hasPrefix: BACK_DIS_URL]) {
        self.navigationItem.leftBarButtonItem = nil;
        return NO;
    }
    
    if([urlString hasPrefix: @"http://113.240.221.145/sms!sms.action"]){
        NSRange range = [urlString rangeOfString:@"#sms="];
        NSString *smsUTF = [urlString substringFromIndex: range.location + range.length];
        
        NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
        NSString *smsBody = [smsUTF stringByReplacingPercentEscapesUsingEncoding:enc];
        //        NSString *smsBody = [smsUTF stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [self sendSMS:smsBody];
        
        return NO;
    }
    
    if (self.navigationItem.leftBarButtonItem == nil && ![qxUrl isEqualToString: urlString] && ![urlString hasPrefix:@"tel://"]){
        UIBarButtonItem *toggleKeyboardItem = [self createBtnWithImg :@"back_dark.png" action:@selector(backPage)];
        self.navigationItem.leftBarButtonItem = toggleKeyboardItem;
    } else if ([qxUrl isEqualToString: urlString]) {
        self.navigationItem.leftBarButtonItem = nil;
    }
    return YES;
}


- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result {
	
        [myWebView stopLoading];
        [myWebView reload];
    
	// Notifies users about errors associated with the interface
    	switch (result) {
    		case MessageComposeResultCancelled:
    			[self alertMessages : @"发送取消"];
    			break;
    		case MessageComposeResultSent:
    			[self alertMessages : @"发送成功"];
    			break;
    		case MessageComposeResultFailed:
    			[self alertMessages : @"发送失败"];
    			break;
    		default:
    			break;
    	}

	[self dismissModalViewControllerAnimated:YES];
}
- (void)alertMessages: (NSString *) messageInfo {
    UIAlertView *alterview = [[UIAlertView alloc] initWithTitle:@"" message:messageInfo  delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
    [alterview show];
}


@end
